package com.onlyphoto.onlymall.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onlyphoto.onlymall.R;
import com.onlyphoto.onlymall.utils.ToastUtil;

/**
 * Created by gaochujia on 2019-07-09.
 */
public class AmountView extends LinearLayout implements View.OnClickListener, TextWatcher, View.OnFocusChangeListener {

    private static final String TAG = "AmountView";
    private int amount = 1; //购买数量
    private int goods_storage = 200; //商品库存

    private OnAmountChangeListener mListener;

    private EditText etAmount;
    private TextView btnDecrease;
    private TextView btnIncrease;

    public AmountView(Context context) {
        this(context, null);
    }

    public AmountView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.view_amount, this);
        etAmount = findViewById(R.id.et_amount);
        btnDecrease = findViewById(R.id.btn_decrease);
        btnIncrease = findViewById(R.id.btn_increase);
        btnDecrease.setOnClickListener(this);
        btnIncrease.setOnClickListener(this);
        etAmount.setOnFocusChangeListener(this);
        etAmount.addTextChangedListener(this);

        TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attrs, R.styleable.AmountView);
        int btnWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btn_width, LayoutParams.WRAP_CONTENT);
        int tvWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tv_width, 80);
        int tvTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tv_textSize, 0);
        int btnTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btn_textSize, 0);
        obtainStyledAttributes.recycle();

        LayoutParams btnParams = new LayoutParams(btnWidth, LayoutParams.MATCH_PARENT);
        btnDecrease.setLayoutParams(btnParams);
        btnIncrease.setLayoutParams(btnParams);
        if (btnTextSize != 0) {
            btnDecrease.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize);
            btnIncrease.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize);
        }

        LayoutParams textParams = new LayoutParams(tvWidth, LayoutParams.MATCH_PARENT);
        etAmount.setLayoutParams(textParams);
        if (tvTextSize != 0) {
            etAmount.setTextSize(tvTextSize);
        }
    }

    public void setOnAmountChangeListener(OnAmountChangeListener onAmountChangeListener) {
        this.mListener = onAmountChangeListener;
    }

    public void setGoods_storage(int goods_storage) {
        this.goods_storage = goods_storage;
    }

    public void setAmount(int count) {
        amount = count;
        etAmount.setText(amount + "");
    }

    public int getAmount(){
        return Integer.parseInt(etAmount.getText().toString());
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_decrease) {
            if (amount > 1) {
                amount--;
                etAmount.setText(amount + "");
                etAmount.setSelection(etAmount.getText().length());
            }
        } else if (i == R.id.btn_increase) {
            if (amount < goods_storage) {
                amount++;
                etAmount.setText(amount + "");
                etAmount.setSelection(etAmount.getText().length());
            } else {
                ToastUtil.INSTANCE.showToast("最多只能买200件哦～");
            }
        }

        etAmount.clearFocus();

        if (mListener != null) {
            mListener.onAmountChange(amount);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.toString().isEmpty()) {
            return;
        }
        amount = Integer.valueOf(s.toString());
        if (amount > goods_storage) {
            ToastUtil.INSTANCE.showToast("最多只能买200件哦～");
            etAmount.setText(String.valueOf(goods_storage));
            etAmount.setSelection(etAmount.getText().length());
            return;
        }
        if (amount < 1) {
            etAmount.setText("1");
            etAmount.setSelection(etAmount.getText().length());
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            String amount = etAmount.getText().toString();
            if (amount.equals("")) {
                etAmount.setText("1");
            }
            if (mListener != null) {
                mListener.onAmountChange(Integer.parseInt(etAmount.getText().toString()));
            }
        }
    }

    public interface OnAmountChangeListener {
        void onAmountChange(int amount);
    }
}
